package chart;

import java.awt.Color;
import java.awt.Shape;
import java.util.Vector;

public class Ring
{

  protected Vector<Segment> SegmentsInfo;
  protected double x;
  protected double y;
  protected double radius;
  protected double ringwidth;
  protected double start;
  protected double end;
  protected Shape[] segments;
  protected boolean canSelect = false;
  protected boolean selected = false;
  protected static final Color SELECTED_COLOR = new Color(0, 0, 0);

  public Ring()
  {
    SegmentsInfo = new Vector<Segment>();
    this.start = 0;
    this.end = 360;
    segments = null;
  }

  public void setStart(double start)
  {
    this.start = start;
  }

  public void setEnd(double end)
  {
    this.end = end;
  }

  public double getValue(int i)
  {
    if (this.segments == null)
    {
      this.createSegments();
    }
    return SegmentsInfo.get(i).Value;
  }

  public int count()
  {
    if (this.segments == null)
    {
      this.createSegments();
    }
    return SegmentsInfo.size();
  }

  public String getLabel(int index)
  {
    return SegmentsInfo.get(index).Label;
  }

  public Color getColor(int index)
  {
    if (SegmentsInfo.get(index).Selected)
    {
      return SELECTED_COLOR;
    }
    return SegmentsInfo.get(index).Color;
  }

  public void setColor(int index, Color color)
  {
    SegmentsInfo.elementAt(index).Color = color;
  }

  public void setCenter(double x, double y)
  {
    this.x = x;
    this.y = y;
  }

  public void setRadius(double radius)
  {
    this.radius = radius;
  }

  public void setRingWidth(double width)
  {
    this.ringwidth = width;
  }

  public void select(int index)
  {
    SegmentsInfo.elementAt(index).Selected = !SegmentsInfo.elementAt(index).Selected;
  }

  public void addItem(String label, double val, Color color)
  {
    SegmentsInfo.add(new Segment(label, color, val));
  }

  public Shape getSegment(int index)
  {
    if (this.segments == null)
    {
      this.createSegments();
    }

    return segments[index];
  }

  public void createSegments()
  {
    if (SegmentsInfo.size() == 0)
    {
      Shape[] shapes = new Shape[1];
      ArcSegment arc = new ArcSegment();
      shapes[0] = arc.Create(x, y, radius, ringwidth, 0, 360);
    } else
    {
      Shape[] shapes = new Shape[SegmentsInfo.size()];
      double sum = 0;
      double span = this.end - this.start;
      for (int i = 0; i < SegmentsInfo.size(); i++)
      {
        sum += SegmentsInfo.get(i).Value;
      }

      double strt = this.start;
      for (int i = 0; i < SegmentsInfo.size(); i++)
      {
        double extent = (SegmentsInfo.get(i).Value / sum) * span;
        ArcSegment arc = new ArcSegment();
        shapes[i] = arc.Create(this.x, this.y, this.radius,
                this.ringwidth, strt, extent);
        strt += extent;
      }

      this.segments = shapes;
    }
  }
}